package com.app.couponsharelife.ui.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import com.app.couponsharelife.data.bean.*
import com.app.couponsharelife.data.remote.RemoteDataSource

class LocalViewModel : BaseViewModel() {

    private var getLifeHomePageFresh = MutableLiveData<FreshBean>()

    private var getLifeHomePageResult: LiveData<ViewDataBean<RemoteResultBean<LifeHomePageBean>>>? = null

    fun getLifeHomePage(): LiveData<ViewDataBean<RemoteResultBean<LifeHomePageBean>>>? {
        if (getLifeHomePageResult == null) {
            getLifeHomePageResult = Transformations.switchMap(getLifeHomePageFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().getLifeHomePage(it.map)
                } else {
                    null
                }
            }
        }
        return getLifeHomePageResult!!
    }

    fun freshGetLifeHomePage(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) getLifeHomePageFresh.value = FreshBean(map, isFresh)
    }

    private var getLifeShopPageFresh = MutableLiveData<FreshBean>()

    private var getLifeShopPageResult: LiveData<ViewDataBean<RemoteResultBean<LifeShopPageBean>>>? = null

    fun getLifeShopPage(): LiveData<ViewDataBean<RemoteResultBean<LifeShopPageBean>>>? {
        if (getLifeShopPageResult == null) {
            getLifeShopPageResult = Transformations.switchMap(getLifeShopPageFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().getLifeShopPage(it.map)
                } else {
                    null
                }
            }
        }
        return getLifeShopPageResult!!
    }

    fun freshGetLifeShopPage(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) getLifeShopPageFresh.value = FreshBean(map, isFresh)
    }

    private var getLifeHotKeywordFresh = MutableLiveData<FreshBean>()

    private var getLifeHotKeywordResult: LiveData<ViewDataBean<RemoteResultBean<List<String>>>>? = null

    fun getLifeHotKeyword(): LiveData<ViewDataBean<RemoteResultBean<List<String>>>>? {
        if (getLifeHotKeywordResult == null) {
            getLifeHotKeywordResult = Transformations.switchMap(getLifeHotKeywordFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().getLifeHotKeyword(it.map)
                } else {
                    null
                }
            }
        }
        return getLifeHotKeywordResult!!
    }

    fun freshGetLifeHotKeyword(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) getLifeHotKeywordFresh.value = FreshBean(map, isFresh)
    }

    private var getLifeShopInfoFresh = MutableLiveData<FreshBean>()

    private var getLifeShopInfoResult: LiveData<ViewDataBean<RemoteResultBean<LifeShopPageBean.RecordsBean>>>? = null

    fun getLifeShopInfo(): LiveData<ViewDataBean<RemoteResultBean<LifeShopPageBean.RecordsBean>>>? {
        if (getLifeShopInfoResult == null) {
            getLifeShopInfoResult = Transformations.switchMap(getLifeShopInfoFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().getLifeShopInfo(it.map)
                } else {
                    null
                }
            }
        }
        return getLifeShopInfoResult!!
    }

    fun freshGetLifeShopInfo(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) getLifeShopInfoFresh.value = FreshBean(map, isFresh)
    }
    private var userCenterFresh = MutableLiveData<FreshBean>()

    private var userCenterResult: LiveData<ViewDataBean<RemoteResultBean<UserCenterBean>>>? = null

    fun userCenter(): LiveData<ViewDataBean<RemoteResultBean<UserCenterBean>>>? {
        if (userCenterResult == null) {
            userCenterResult = Transformations.switchMap(userCenterFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().userCenter(it.map)
                } else {
                    null
                }
            }
        }
        return userCenterResult!!
    }

    fun freshUserCenter(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) userCenterFresh.value = FreshBean(map, isFresh)
    }

    private var getLifeCardInfoFresh = MutableLiveData<FreshBean>()

    private var getLifeCardInfoResult: LiveData<ViewDataBean<RemoteResultBean<LifeShopPageBean.CardsBean>>>? = null

    fun getLifeCardInfo(): LiveData<ViewDataBean<RemoteResultBean<LifeShopPageBean.CardsBean>>>? {
        if (getLifeCardInfoResult == null) {
            getLifeCardInfoResult = Transformations.switchMap(getLifeCardInfoFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().getLifeCardInfo(it.map)
                } else {
                    null
                }
            }
        }
        return getLifeCardInfoResult!!
    }

    fun freshGetLifeCardInfo(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) getLifeCardInfoFresh.value = FreshBean(map, isFresh)
    }

    private var getLifeNewCardFresh = MutableLiveData<FreshBean>()

    private var getLifeNewCardResult: LiveData<ViewDataBean<RemoteResultBean<LifeShopPageBean.CardsBean>>>? = null

    fun getLifeNewCard(): LiveData<ViewDataBean<RemoteResultBean<LifeShopPageBean.CardsBean>>>? {
        if (getLifeNewCardResult == null) {
            getLifeNewCardResult = Transformations.switchMap(getLifeNewCardFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().getLifeNewCard(it.map)
                } else {
                    null
                }
            }
        }
        return getLifeNewCardResult!!
    }

    fun freshGetLifeNewCard(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) getLifeNewCardFresh.value = FreshBean(map, isFresh)
    }

    private var payMoneyCalculateFresh = MutableLiveData<FreshBean>()

    private var payMoneyCalculateResult: LiveData<ViewDataBean<RemoteResultBean<PayMoneyBean>>>? = null

    fun payMoneyCalculate(): LiveData<ViewDataBean<RemoteResultBean<PayMoneyBean>>>? {
        if (payMoneyCalculateResult == null) {
            payMoneyCalculateResult = Transformations.switchMap(payMoneyCalculateFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().payMoneyCalculate(it.map)
                } else {
                    null
                }
            }
        }
        return payMoneyCalculateResult!!
    }

    fun freshPayMoneyCalculate(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) payMoneyCalculateFresh.value = FreshBean(map, isFresh)
    }
}